package com.example.process;

import com.example.bean.WaterSenSorFunction;
import com.example.bean.WaterSensor;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.TimerService;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * ClassName: WindowRdeuceDemo
 * Package: com.example.window
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-19
 * Time: 17:41
 */

//设置定时器
public class KeyedProcessTimeDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env =
                StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> data = env.socketTextStream("hadoop102", 7777)
                .map(new WaterSenSorFunction());

        //指定watermark策略
        WatermarkStrategy<WaterSensor> watermarkStrategy = WatermarkStrategy
                .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3)) //乱序的 有界限的 说明可以等 等多久
                .withTimestampAssigner(new SerializableTimestampAssigner<WaterSensor>() {
                    @Override
                    public long extractTimestamp(WaterSensor element, long recordTimestamp) {
                        System.out.println("数据=" + element + "recordTs=" + recordTimestamp);
                        return element.getTs() * 1000L;
                    }
                });

        SingleOutputStreamOperator<WaterSensor> waterSensorSingleOutputStreamOperator =
                data.assignTimestampsAndWatermarks(watermarkStrategy);


        KeyedStream<WaterSensor, String> sensorKS =
                waterSensorSingleOutputStreamOperator.keyBy(value -> value.getId());

        //Process Keyed
        SingleOutputStreamOperator<String> process = sensorKS.process(new KeyedProcessFunction<String, WaterSensor, String>() {
            /**
             * 来一条数据调用一次
             * @param value
             * @param ctx
             * @param out
             * @throws Exception
             */
            @Override
            public void processElement(WaterSensor value, Context ctx, Collector<String> out) throws Exception {
                //数据中提取出来的事件时间 如果没有就是null
                Long ts = ctx.timestamp();

                //定时器
                TimerService timerService = ctx.timerService();
                //注册事件 事件时间
                timerService.registerEventTimeTimer(5000);
                System.out.println("当前时间是 ts " + ts + "注册了一个5秒的定时器");
//                //注册事件 处理时间
//                timerService.registerProcessingTimeTimer();
//
//                //删除定时器 事件时间
//                timerService.deleteEventTimeTimer();
//                //删除定时器 处理时间
//                timerService.deleteProcessingTimeTimer();
//
//                //获取当前处理时间 也就是系统时间
//                final long l = timerService.currentProcessingTime();
//                //获取当前watermark
//                long wm = timerService.currentWatermark();
            }

            /**
             * 时间进展到定时器注册的时间 调用该方法
             * @param timestamp 当前的时间进展
             * @param ctx
             * @param out
             * @throws Exception
             */
            @Override
            public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
                super.onTimer(timestamp, ctx, out);
                System.out.println("现在时间是 " + timestamp + "定时器触发");
            }
        });

        process.print();


        env.execute();
    }

}
